package com.glory.erp.generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import com.glory.erp.invoice.operdetail.bean.OperDetail;

/**
 * @Description: 代码生成器
 * @Title: Test.java
 * @Author: 樊双峰
 * @Date: 2018年5月20日 下午3:54:42
 * @Version V1.0
 */
public class Generator {

	private Class clazz;
	private String className; // Emp
	private String l; // e
	private String s; // emp
	private String pkg; // cn.itcast.erp.auth.emp
	private String dir; // cn/itcast/erp/auth/emp/vo

	public static void main(String[] args) throws Exception {
		// Role,Resources,Dept,Menu
		// Supplier,GoodsType,Goods,
		// Order,OrderDetail
		// Store,StoreDetail,OperDetail
		// 生成入口
		// new Generator();
		System.out.println("struts.xml未进行映射");
		System.out.println("HbmXml未添加关联关系");
		System.out.println("QueryModel未添加自定义范围查询条件");
		System.out.println("DaoImpl中未对自定义查询条件形式条件设置");
	}

	public Generator(Class clazz) throws Exception {
		this.clazz = clazz;
		dataInit();
		generatorDirectory();
		generatorQueryModel();
		generatorHbmXml();
		generatorDao();
		generatorImpl();
		generatorService();
		generatorServiceImpl();
		generatorAction();
		generatorApplicationContextXml();
	}

	// 1 初始化路径
	private void dataInit() {
		className = clazz.getSimpleName(); // Demo
		String first = className.substring(0, 1); // D
		l = first.toLowerCase(); // d
		s = l + className.substring(1); // demo
		String rootPkg = clazz.getPackage().getName(); // com.glory.erp.auth.emp.bean
		pkg = rootPkg.substring(0, rootPkg.length() - 5); // com.glory.erp.auth.emp
		dir = pkg.replace(".", "/"); // com/glory/erp/auth/emp
	}

	// 2 创建文件
	private void generatorDirectory() {
		File f = new File("src/" + dir + "/service");
		f.mkdirs();

		f = new File("src/" + dir + "/service/impl");
		f.mkdirs();

		f = new File("src/" + dir + "/dao");
		f.mkdirs();

		f = new File("src/" + dir + "/dao/impl");
		f.mkdirs();

		f = new File("src/" + dir + "/action");
		f.mkdirs();
	}

	// 3 Query
	private void generatorQueryModel() throws Exception {
		// 1.创建文件
		File f = new File("src/" + dir + "/bean/" + className + "Query.java");

		// 判断：如果该文件存在，终止操作
		if (f.exists()) {
			return;
		}

		f.createNewFile();
		// 2.IO写入内容
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("package " + pkg + ".bean;");
		bw.newLine();

		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseQueryModel;");
		bw.newLine();

		bw.newLine();

		bw.write("public class " + className + "Query extends " + className
				+ " implements BaseQueryModel{");
		bw.newLine();

		bw.write("	// TODO 添加自定义查询条件");
		bw.newLine();

		bw.write("}");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 4.hbm.xml
	private void generatorHbmXml() throws Exception {
		// 1.创建文件
		File f = new File("src/" + dir + "/bean/" + className + ".hbm.xml");

		if (f.exists()) {
			return;
		}

		f.createNewFile();
		// 2.IO写入内容
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		bw.newLine();

		bw.write("<!DOCTYPE hibernate-mapping PUBLIC");
		bw.newLine();

		bw.write("        '-//Hibernate/Hibernate Mapping DTD 3.0//EN'");
		bw.newLine();

		bw.write("        'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>");
		bw.newLine();

		bw.write("<hibernate-mapping>");
		bw.newLine();

		bw.write("	<class name=\"" + pkg + ".bean." + className + "\" table=\""
				+ s + "\">");
		bw.newLine();

		bw.write("        <id name=\"uuid\">");
		bw.newLine();

		bw.write("            <generator class=\"native\" />");
		bw.newLine();

		bw.write("        </id>");
		bw.newLine();

		// hibernate的映射配置文件中要对原始模型类中的属性进行配置，反射获取所有字段
		Field[] fds = clazz.getDeclaredFields();
		for (Field fd : fds) {
			// 如果字段的修饰符是private，生成
			if (fd.getModifiers() == Modifier.PRIVATE
					&& !fd.getName().equals("uuid")) {
				// 如果是关联关系不生成,不是关联关系(Long,Integer,Double,String)
				if (fd.getType().equals(String.class)
						|| fd.getType().equals(Long.class)
						|| fd.getType().equals(Integer.class)
						|| fd.getType().equals(Double.class)
						) {
						if(!fd.getName().endsWith("View")){							
							bw.write("        <property name=\"" + fd.getName()
									+ "\"/>");
							bw.newLine();
						}
				}
			}
		}

		bw.write("    </class>");
		bw.newLine();

		bw.write("</hibernate-mapping>");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 5.Dao
	private void generatorDao() throws Exception {
		File f = new File("src/" + dir + "/dao/" + className + "Dao.java");
		if (f.exists()) {
			return;
		}
		f.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("package " + pkg + ".dao;");
		bw.newLine();

		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + ";");
		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseDao;");
		bw.newLine();

		bw.newLine();

		bw.write("public interface " + className + "Dao extends BaseDao<"
				+ className + "> {");
		bw.newLine();

		bw.newLine();

		bw.write("}");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 6.DaoImpl
	private void generatorImpl() throws Exception {
		File f = new File("src/" + dir + "/dao/impl/" + className
				+ "DaoImpl.java");
		if (f.exists()) {
			return;
		}
		f.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("package " + pkg + ".dao.impl;");
		bw.newLine();

		bw.newLine();

		bw.write("import org.hibernate.criterion.DetachedCriteria;");
		bw.newLine();

		bw.write("import org.hibernate.criterion.Restrictions;");
		bw.newLine();

		bw.newLine();

		bw.write("import " + pkg + ".dao." + className + "Dao;");
		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + ";");
		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + "Query;");
		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseDaoImpl;");
		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseQueryModel;");
		bw.newLine();

		bw.newLine();

		bw.write("public class " + className + "DaoImpl extends BaseDaoImpl<"
				+ className + "> implements " + className + "Dao{");
		bw.newLine();

		bw.newLine();

		bw.write("	public void doQbc(DetachedCriteria dc,BaseQueryModel qm){");
		bw.newLine();

		bw.write("		" + className + "Query " + l + "qm = (" + className
				+ "Query)qm;");
		bw.newLine();

		bw.write("		// TODO 添加自定义查询条件");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("}");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 7.service
	private void generatorService() throws Exception {
		File f = new File("src/" + dir + "/service/" + className
				+ "Service.java");
		if (f.exists()) {
			return;
		}
		f.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("package " + pkg + ".service;");
		bw.newLine();

		bw.newLine();

		bw.write("import org.springframework.transaction.annotation.Transactional;");
		bw.newLine();

		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + ";");
		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseService;");
		bw.newLine();

		bw.newLine();

		bw.write("@Transactional");
		bw.newLine();

		bw.write("public interface " + className
				+ "Service extends BaseService<" + className + ">{");
		bw.newLine();

		bw.newLine();

		bw.write("}");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 8.serviceImpl
	private void generatorServiceImpl() throws Exception {
		File f = new File("src/" + dir + "/service/impl/" + className
				+ "ServiceImpl.java");
		if (f.exists()) {
			return;
		}
		f.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("package " + pkg + ".service.impl;");
		bw.newLine();

		bw.newLine();

		bw.write("import java.io.Serializable;");
		bw.newLine();

		bw.write("import java.util.List;");
		bw.newLine();

		bw.newLine();

		bw.write("import " + pkg + ".service." + className + "Service;");
		bw.newLine();

		bw.write("import " + pkg + ".dao." + className + "Dao;");
		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + ";");
		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseQueryModel;");
		bw.newLine();

		bw.newLine();

		bw.write("public class " + className + "ServiceImpl implements "
				+ className + "Service{");
		bw.newLine();

		bw.write("	private " + className + "Dao " + s + "Dao;");
		bw.newLine();

		bw.write("	public void set" + className + "Dao(" + className + "Dao "
				+ s + "Dao) {");
		bw.newLine();

		bw.write("		this." + s + "Dao = " + s + "Dao;");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public void save(" + className + " " + s + ") {");
		bw.newLine();

		bw.write("		" + s + "Dao.save(" + s + ");");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public void update(" + className + " " + s + ") {");
		bw.newLine();

		bw.write("		" + s + "Dao.update(" + s + ");");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public void delete(" + className + " " + s + ") {");
		bw.newLine();

		bw.write("		" + s + "Dao.delete(" + s + ");");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public " + className + " findById(Serializable uuid) {");
		bw.newLine();

		bw.write("		return " + s + "Dao.findById(uuid);");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public List<" + className + "> findAll() {");
		bw.newLine();

		bw.write("		return " + s + "Dao.findAll();");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public List<"
				+ className
				+ "> findAll(BaseQueryModel qm, Integer pageNum,Integer pageCount) {");
		bw.newLine();

		bw.write("		return " + s + "Dao.findAll(qm,pageNum,pageCount);");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	public Integer getCount(BaseQueryModel qm) {");
		bw.newLine();

		bw.write("		return " + s + "Dao.getCount(qm);");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("}");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 9.Action
	private void generatorAction() throws Exception {
		File f = new File("src/" + dir + "/action/" + className + "Action.java");
		if (f.exists()) {
			return;
		}
		f.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("package " + pkg + ".action;");
		bw.newLine();

		bw.newLine();

		bw.write("import java.util.List;");
		bw.newLine();

		bw.newLine();

		bw.write("import " + pkg + ".service." + className + "Service;");
		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + ";");
		bw.newLine();

		bw.write("import " + pkg + ".bean." + className + "Query;");
		bw.newLine();

		bw.write("import com.glory.erp.auth.base.BaseAction;");
		bw.newLine();

		bw.newLine();

		bw.write("public class " + className + "Action extends BaseAction{");
		bw.newLine();

		bw.write("	public " + className + " " + s + " = new " + className
				+ "();");
		bw.newLine();

		bw.write("	public " + className + "Query " + s + "Query = new "
				+ className + "Query();");
		bw.newLine();

		bw.newLine();

		bw.write("	private " + className + "Service " + s + "Service;");
		bw.newLine();

		bw.write("	public void set" + className + "Service(" + className
				+ "Service " + s + "Service) {");
		bw.newLine();

		bw.write("		this." + s + "Service = " + s + "Service;");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	//列表");
		bw.newLine();

		bw.write("	public String list(){");
		bw.newLine();

		bw.write("		setDataTotal(" + s + "Service.getCount(" + s + "Query));");
		bw.newLine();

		bw.write("		List<" + className + "> " + s + "List = " + s
				+ "Service.findAll(" + s + "Query,pageNum,pageCount);");
		bw.newLine();

		bw.write("		put(\"" + s + "List\", " + s + "List);");
		bw.newLine();

		bw.write("		return LIST;");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	//到添加");
		bw.newLine();

		bw.write("	public String addView(){");
		bw.newLine();

		bw.write("		if(" + s + ".getUuid()!=null){");
		bw.newLine();

		bw.write("			" + s + " = " + s + "Service.findById(" + s
				+ ".getUuid());");
		bw.newLine();

		bw.write("		}");
		bw.newLine();

		bw.write("		return ADD;");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	//添加");
		bw.newLine();

		bw.write("	public String saveAndUpdate(){");
		bw.newLine();

		bw.write("		if(" + s + ".getUuid() == null){");
		bw.newLine();

		bw.write("			" + s + "Service.save(" + s + ");");
		bw.newLine();

		bw.write("		}else{");
		bw.newLine();

		bw.write("			" + s + "Service.update(" + s + ");");
		bw.newLine();

		bw.write("		}");
		bw.newLine();

		bw.write("		return TO_LIST;");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("	//删除");
		bw.newLine();

		bw.write("	public String delete(){");
		bw.newLine();

		bw.write("		" + s + "Service.delete(" + s + ");");
		bw.newLine();

		bw.write("		return TO_LIST;");
		bw.newLine();

		bw.write("	}");
		bw.newLine();

		bw.newLine();

		bw.write("}");
		bw.newLine();

		bw.flush();
		bw.close();
	}

	// 10.applicationContext.xml
	private void generatorApplicationContextXml() throws Exception {
		File f = new File("resources/applicationContext-" + s + ".xml");
		if (f.exists()) {
			return;
		}
		f.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));

		bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		bw.newLine();

		bw.write("<beans xmlns=\"http://www.springframework.org/schema/beans\"");
		bw.newLine();

		bw.write("	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
		bw.newLine();

		bw.write("	xsi:schemaLocation=\"");
		bw.newLine();

		bw.write("		http://www.springframework.org/schema/beans ");
		bw.newLine();

		bw.write("		http://www.springframework.org/schema/beans/spring-beans.xsd");
		bw.newLine();

		bw.write("		\"> ");
		bw.newLine();

		bw.write("	<!-- Action -->");
		bw.newLine();

		bw.write("	<bean id=\"" + s + "Action\" class=\"" + pkg + ".action." + className
				+ "Action\" scope=\"prototype\">");
		bw.newLine();

		bw.write("		<property name=\"" + s + "Service\" ref=\"" + s + "Service\"/>");
		bw.newLine();

		bw.write("	</bean>");
		bw.newLine();

		bw.write("	<!-- Service -->");
		bw.newLine();

		bw.write("	<bean id=\"" + s + "Service\" class=\"" + pkg + ".service.impl."
				+ className + "ServiceImpl\">");
		bw.newLine();

		bw.write("		<property name=\"" + s + "Dao\" ref=\"" + s + "Dao\"/>");
		bw.newLine();

		bw.write("	</bean>");
		bw.newLine();

		bw.write("	<!-- Dao -->");
		bw.newLine();

		bw.write("	<bean id=\"" + s + "Dao\" class=\"" + pkg + ".dao.impl." + className
				+ "DaoImpl\">");
		bw.newLine();

		bw.write("		<property name=\"sessionFactory\" ref=\"sessionFactory\"/>");
		bw.newLine();

		bw.write("	</bean>");
		bw.newLine();

		bw.write("</beans>");
		bw.newLine();

		bw.flush();
		bw.close();
	}
}